var params = {
			padding: 20, // 棋盘内边距
			lattice: 45, // 棋盘格子宽高 50x50
			bgColor: "#f9a956",
			size: 15
	}
	var c = document.getElementById('gob_board');
	var ctx = c.getContext('2d');
	var arr;
	// 绘制棋盘
	function buildGobBoard() {
		
		var size = params.size;
		var padding = params.padding;
		var lattice = params.lattice;
		var widthHeight = lattice * 14 + padding * 2; // 棋盘的宽高
		
		arr = new Array(size); // 棋盘二维数组
		
		c.setAttribute('width', widthHeight);
		c.setAttribute('height', widthHeight);
		ctx.fillStyle = params.bgColor;
		ctx.fillRect(0,0,widthHeight,widthHeight);
		
		for(var i = 0; i < size; i++){
			ctx.moveTo(padding + i * lattice , padding);
			ctx.lineTo(padding + i * lattice , (widthHeight-padding));
			ctx.stroke();
			ctx.moveTo(padding , padding + i * lattice);
			ctx.lineTo((widthHeight-padding) , padding + i * lattice);
			ctx.stroke();
			arr[i] = Array.apply(null, Array(size)).map(() => 0);
	    }
		
	}
	
	// 根据鼠标点击的坐标获取画布上的坐标
	function getLocation(x, y) {
		var bbox = c.getBoundingClientRect();
		return {
			x: (x - bbox.left) * (c.width / bbox.width),
			y: (y - bbox.top) * (c.height / bbox.height)
		};
	}
	
	// 预判落子位置
	function expect(location) {
		var xNum = parseInt(location.x / params.lattice);
		var yNum = parseInt(location.y / params.lattice);
		
		var x = xNum * params.lattice + params.padding;
		var y = yNum * params.lattice + params.padding;
		
		
		/* $('#mask').show();
		$('#mask').css({"top": y-10, "left": x-10}); */
		
	}
	
	// 落子
	function execute(location) {
		var xNum = parseInt(location.x / params.lattice);
		var yNum = parseInt(location.y / params.lattice);
		// 填充二维数组
		if($blackOrWhite == 'white') {
			arr[yNum][xNum] = 1;
		}else {
			arr[yNum][xNum] = 2;
		}
		
		// 通过websocket传递
		sendMessage(2, '', $blackOrWhite, xNum, yNum);
		
		// 电脑AI
//		setTimeout(function(){
//			calc(xNum, yNum);
//		}, 500);
		
	}
	
	
	// 绘制棋子
	function drawPiece(xNum, yNum, type) {
		var x = (xNum * params.lattice + params.padding)-18; // ps: 18为棋子图片的半径
		var y = (yNum * params.lattice + params.padding)-18;
		
		var img=document.createElement("img");
		if( 'white' === type ) {
			img.src = '/img/white1.png';
		}else {
			img.src = '/img/black1.png';
		}
		img.onload = function (){
			ctx.drawImage(img, x, y);
		}
		
		
		setTimeout(function() {
			// 计算结果
			var result = getResult(xNum, yNum);
			
			if(result == 1) {
				sendMessage(0, '白棋获胜，游戏结束！');
			}else if(result == 2) {
				sendMessage(0, '黑棋获胜，游戏结束！');
			}
		}, 500);
	}
	
	// 电脑AI计算黑棋落子位置 8
	function calc(xNum, yNum) {
		var rnd = Math.floor(Math.random()*(8-1+1)+1);
		var len = arr.length;
		var x,y;
		switch(rnd) {
		case 1: // 左
			try{
				x = xNum-1;
				y = yNum;
				// ps：“白棋是1 黑棋是2 空白是0 这里判断<1的意义是 必须让棋子落在空白棋盘上 不能覆盖已有棋子”。
				if( x >= 0 && arr[y][x] < 1 ) { 
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 2: // 右
			try{
				x = xNum+1;
				y = yNum;
				if( x <= len && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 3: // 上
			try{
				x = xNum;
				y = yNum-1;
				if( y >= 0 && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 4: // 下
			try{
				x = xNum;
				y = yNum+1;
				if( y <= len && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 5: // 左上
			try{
				x = xNum-1;
				y = yNum-1;
				if( ( x >= 0 && y >= 0 ) && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 6: // 右下
			try{
				x = xNum+1;
				y = yNum+1;
				if( ( x <= len && y <= len ) && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 7: // 右上
			try{
				x = xNum+1;
				y = yNum-1;
				if( ( x <= len && y >=0 ) && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		case 8: // 左下
			try{
				x = xNum-1;
				y = yNum+1;
				debugger;
				if( ( x >= 0 && y <= len ) && arr[y][x] < 1 ) {
					arr[y][x] = 2;
					break;
				}else {
					calc(xNum, yNum);
				}
			}catch(e) {
				calc(xNum, yNum);
			}
		
		}
		
		drawPiece(x, y, 'black');
		
	}
	
	// 获取结果
	function getResult(x, y) {
		//debugger;
		try {
			// 方式1 横向判断
			for(var i = 0; i < 10; i++) {
				
				if( arr[y][i] == 1 && arr[y][i+1] == 1 && arr[y][i+2] == 1 && 
						arr[y][i+3] == 1 && arr[y][i+4] == 1) {
					return 1;
				}
				if( arr[y][i] == 2 && arr[y][i+1] == 2 && arr[y][i+2] == 2 && 
						arr[y][i+3] == 2 && arr[y][i+4] == 2) {
					return 2;
				}
			}
			// 方式2 竖向判断
			for(var i = 0; i < 10; i++) {
				if( arr[i][x] == 1 && arr[i+1][x] == 1 && arr[i+2][x] == 1 && 
						arr[i+3][x] == 1 && arr[i+4][x] == 1) {
					return 1;
				}
				if( arr[i][x] == 2 && arr[i+1][x] == 2 && arr[i+2][x] == 2 && 
						arr[i+3][x] == 2 && arr[i+4][x] == 2) {
					return 2;
				}
			}
			// 方式3 斜向左到右(找头公式：x-y=xy)
			x1 = x, y1 = y;
			var h = parseInt(x1)-parseInt(y1);
			// 判断如果是负数则是Y轴，反之则是X轴
			if( h < 0 ) {
				y1 = Math.abs(h);
				x1 = 0;
			}else if( h > 0 ) {
				x1 = h;
				y1 = 0;
			}else {
				x1 = h;
				y1 = h;
			}
			for(var i = 0; i < Math.abs(10-(y1+1)); i++) {
				if( arr[i+y1][i+x1] == 1 && arr[i+y1+1][i+x1+1] == 1 && arr[i+y1+2][i+x1+2] == 1 && 
						arr[i+y1+3][i+x1+3] == 1 && arr[i+y1+4][i+x1+4] == 1) {
					return 1;
				}
				if( arr[i+y1][i+x1] == 2 && arr[i+y1+1][i+x1+1] == 2 && arr[i+y1+2][i+x1+2] == 2 && 
						arr[i+y1+3][i+x1+3] == 2 && arr[i+y1+4][i+x1+4] == 2) {
					return 2;
				}
			}
			// 方式4 斜向右到左
			x2 = x, y2 = y;
			var j = parseInt(x2)+parseInt(y2);
			if( j >= 14 ) {
				x2 = 14;
				y2 = Math.abs(14-j);
			}else if( j < 14 ) {
				x2 = j;
				y2 = 0;
			}
			for(var i = 0; i < Math.abs(10-(y2+1)); i++) {
				if( arr[y2+i][x2-i] == 1 && arr[y2+i+1][x2-i-1] == 1 && arr[y2+i+2][x2-i-2] == 1 && 
						arr[y2+i+3][x2-i-3] == 1 && arr[y2+i+4][x2-i-4] == 1) {
					return 1;
				}
				if( arr[y2+i][x2-i] == 2 && arr[y2+i+1][x2-i-1] == 2 && arr[y2+i+2][x2-i-2] == 2 && 
						arr[y2+i+3][x2-i-3] == 2 && arr[y2+i+4][x2-i-4] == 2) {
					return 2;
				}
			}
		} catch(e) {
			console.log(e);
		}
	}
	
	buildGobBoard();
	
	function showSysMsg(msg) {
		$('#sys_message').append('<div class="sys_msg">> 系统消息：'+msg+'</div>');
		$('#sys_message').scrollTop($('#sys_message')[0].scrollHeight );
	}
	
	// 鼠标移动事件
	c.onmousemove = function(e) {
		var location = getLocation(e.clientX, e.clientY);
		//console.log("坐标："+location.x+"  "+location.y);
		expect(location);
	}
	
	// 鼠标点击画布事件
	c.onclick = function(e) {
		/*if(!isOpen) {
			showSysMsg('游戏未开始哦~');
			return;
		}
		var whiteChecked = $('.white').attr('checked');
		var blackChecked = $('.black').attr('checked');
		if( typeof(whiteChecked) == 'undefined' && typeof(blackChecked) == 'undefined' ) {
			showSysMsg('你还没有选择黑棋还是白棋哦~');
			return;
		}else if( whiteChecked != 'checked' || blackChecked != 'checked' ) {
			showSysMsg('请等待对手选择阵营~');
			return;
		}*/
		var location = getLocation(e.clientX, e.clientY);
		
		execute(location);
		
	}
	$('#btn_calc').on('click', function() {
		console.log(arr);
	});
	
	$('#restart').on('click', function() {
		location.href = '';
		//buildGobBoard();
	});